/*
 * \brief  Client-side I/O-port session interface
 * \author Christian Helmuth
 * \date   2007-04-17
 */

/*
 * Copyright (C) 2007-2009 Christian Helmuth
 * Genode Labs, Feske & Helmuth Systementwicklung GbR
 *
 * This file is part of the Genode OS framework, which is distributed
 * under the terms of the GNU General Public License version 2.
 */

#ifndef _INCLUDE__IO_PORT_SESSION__CLIENT_H_
#define _INCLUDE__IO_PORT_SESSION__CLIENT_H_

#include <io_port_session/capability.h>
#include <base/ipc.h>
#include <base/lock.h>

namespace Genode {

	class Io_port_session_client : public Io_port_session
	{
		private:

			Msgbuf<32> _snd_msg, _rcv_msg;
			Ipc_client  _ipc_client;
			Lock        _lock;

		public:

			/**
			 * Constructor
			 */
			Io_port_session_client(Io_port_session_capability session)
			: _ipc_client(session, &_snd_msg, &_rcv_msg) { }


			/*******************************
			 ** Io_port session interface **
			 *******************************/

			unsigned char inb(unsigned short address)
			{
				unsigned char value = 0;
				Lock::Guard lock_guard(_lock);

				_ipc_client << INB << address
				            << IPC_CALL
				            >> value;

				return value;
			}

			unsigned short inw(unsigned short address)
			{
				unsigned short value = 0;
				Lock::Guard lock_guard(_lock);

				_ipc_client << INW << address
				            << IPC_CALL
				            >> value;

				return value;
			}

			unsigned inl(unsigned short address)
			{
				unsigned value = 0;
				Lock::Guard lock_guard(_lock);

				_ipc_client << INL << address
				            << IPC_CALL
				            >> value;

				return value;
			}

			void outb(unsigned short address, unsigned char value)
			{
				Lock::Guard lock_guard(_lock);

				_ipc_client << OUTB << address << value
				            << IPC_CALL;
			}

			void outw(unsigned short address, unsigned short value)
			{
				Lock::Guard lock_guard(_lock);

				_ipc_client << OUTW << address << value
				            << IPC_CALL;
			}

			void outl(unsigned short address, unsigned value)
			{
				Lock::Guard lock_guard(_lock);

				_ipc_client << OUTL << address << value
				            << IPC_CALL;
			}
	};
}

#endif /* _INCLUDE__IO_PORT_SESSION__CLIENT_H_ */
